package com.baichang.taoshunda.utils;


import com.baichang.framework.excelption.ServiceException;
import com.baichang.framework.utils.http.HttpUtils;
import com.baichang.taoshunda.entity.em.UserType;
import net.sf.json.JSONObject;

import java.util.HashMap;
import java.util.Map;

/**
 * 环信工具类
 * Created by Dylan on 2015/12/8.
 */
public class EasemobUtils {

    static Map<String, Object> params;
    ;

    static Map<String, Object> returnMap = new HashMap<String, Object>();

    /**
     * 无参构造
     */
//    public EasemobUtils() {
//        if (params == null)
//            params = new HashMap<String, Object>();
//    }

    /**
     * 注册环信
     *
     * @param userId   本地系统的用户唯一标识
     * @param userType 用户身份
     * @param photo    用户头像
     * @param name     用户昵称
     * @return
     */

    public static boolean register(String userId, UserType userType, String photo, String name) throws ServiceException {
        params = new HashMap<String, Object>();
        params.put("userId", userId);
        params.put("userType", userType.getCode());
        params.put("photo", photo);
        params.put("name", name);
        try {
            returnMap = executePost(EasemobContants.registerUrl, params);
            return ConvertTypeUtils.obj2Boolean(returnMap.get("data"));
        } catch (Exception e) {
            throw new ServiceException("朋友注册失败");
        }

    }

    /**
     * 获取用户信息
     *
     * @param userId   本地系统用户编号
     * @param userType
     * @return
     */
    public static Map<String, Object> getUser(String userId, UserType userType) throws ServiceException {
        params = new HashMap<String, Object>();
        params.put("userId", userId);
        params.put("userType", userType.getCode());
        try {
            returnMap = executePost(EasemobContants.getUserUrl, params);
            return (Map<String, Object>) returnMap.get("data");
        } catch (Exception e) {
            throw new ServiceException("获取失败");
        }

    }


    /**
     * 修改环信信息
     *
     * @param userKid  环信系统中的用户编号
     * @param userType 用户身份
     * @param name     用户昵称
     * @param photo    用户头像
     * @return
     */
    public static boolean update(String userKid, UserType userType, String name, String photo) throws ServiceException {
        params = new HashMap<String, Object>();
        params.put("userKid", userKid);
        params.put("userType", userType.getCode());
        params.put("name", name);
        params.put("photo", photo);
        try {
            returnMap = executePost(EasemobContants.updateUserUrl, params);
            return ConvertTypeUtils.obj2Boolean(returnMap.get("data"));
        } catch (Exception e) {
            throw new ServiceException("修改环信信息失败");
        }

    }

    /**
     * 删除环信用户
     *
     * @param userKid 环信系统用户编号
     * @return
     */
    public static boolean delete(String userKid) throws ServiceException {
        params = new HashMap<String, Object>();
        params.put("userKid", userKid);
        try {
            returnMap = executePost(EasemobContants.deleteUserUrl, params);
            return ConvertTypeUtils.obj2Boolean(returnMap.get("data"));
        } catch (Exception e) {
            throw new ServiceException("删除环信用户失败");
        }

    }

    /**
     * 请求接口
     *
     * @param actionUrl 请求的控制器地址
     * @param params    请求参数
     */
    private static Map<String, Object> executePost(String actionUrl, Map<String, Object> params) throws ServiceException {
        String url = EasemobContants.url;
        Map<String, Object> action = new HashMap<String, Object>();
        action.put("action", actionUrl);
        action.put("params", params);
        JSONObject jsonObject = JSONObject.fromObject(action);
        String json = jsonObject.toString();
        String data = null;
        String result = null;
        try {
            //请求加密
//            data = AES.encode(json, Constants.REQUEST_KEY);

            result = HttpUtils.postJson(url, json);
            System.out.println(result);

            // 解密
//            result = AES.decode(result, Constants.RESPONSE_KEY);
        } catch (Exception e) {
            e.printStackTrace();
        }
        Map<String, Object> resultMap;
        try {
            resultMap = JSONObject.fromObject(result);
        } catch (Exception e) {
            resultMap = new HashMap<>();
        }
        Object resultObj = resultMap.get("res");
        if (resultObj == null)
            throw new ServiceException("请求环信失败");
        Map<String, Object> returnMap = (Map<String, Object>) resultObj;
        Integer returnCode = ConvertTypeUtils.obj2Integer(returnMap.get("code"));
        if (returnCode != 40000)
            throw new ServiceException(returnMap.get("msg").toString());
        return returnMap;
    }

    public static String getEmIdByUserTypeAndId(String userId, UserType userType) throws Exception {
        Map<String, Object> map = getUser(userId, userType);
        if (map != null && map.size() > 0) {
            return (String) map.get("emId");
        }
        return "";
    }

    public static void main(String[] args) throws Exception {
//        System.out.println(getEmIdByUserTypeAndId("209", UserType.商家));
        EasemobUtils.register("63", UserType.商家, "", "");
        Map<String, Object> map = getUser("63", UserType.商家);
        System.out.println(map.toString());
//        System.out.println(map.toString());
//        Map<String, Object> action = new HashMap<String, Object>();
//        action.put("action", "user/register");
//        Map<String, Object> params = new HashMap<String, Object>();
//        params.put("userId", "3");
//        params.put("userType", "depotUser");
//        params.put("name", "杜欣�?");
//        params.put("photo", "20151215/14501503560272912/1450150382224.jpg");
//        action.put("params", params);
//        JSONObject jsonObject = JSONObject.fromObject(action);
//        String json = jsonObject.toString();
//        com.tencent.common.http.HttpUtils.postString(EasemobContants.url, json);
//        Map<String, Object> action = new HashMap<String, Object>();
//        action.put("action", "user/get");
//        Map<String, Object> params = new HashMap<String, Object>();
//        params.put("userId", "3");
//        params.put("userType", "depotUser");
//        params.put("name", "杜欣�?");
//        params.put("photo", "20151215/14501503560272912/1450150382224.jpg");
//        action.put("params", params);
//        JSONObject jsonObject = JSONObject.fromObject(action);
//        String json = jsonObject.toString();
//        com.tencent.common.http.HttpUtils.postString(EasemobContants.url, json);
    }

}
